package com.younion.action.consumer.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.younion.action.consumer.AbstractRequestConsumer;
import com.younion.action.interactive.mobile.ApiConstant;
import com.younion.action.interactive.mobile.ResultObject;
import com.younion.business.IFavourableService;
import com.younion.business.IMemberCardService;
import com.younion.model.FavOperateResult;
import com.younion.util.HttpRequestUtils;
import com.younion.util.JsonUtils;
import com.younion.vo.FavourableEntityVO;
import com.younion.vo.FavourableVO;

public class FavourableUseConsumer extends AbstractRequestConsumer {

	private IFavourableService favourableService;
	private IMemberCardService memberCardService;

	@Override
	public String execute(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		return useFavourable(request, response);
	}

	public String useFavourable(HttpServletRequest request,
			HttpServletResponse response) {
		String fav_id = request.getParameter("fav_id");
		String user_token = request.getParameter("user_token");
		if (!HttpRequestUtils.checkNotEmptyParam("user_token", user_token,
				response)) {
			return null;
		}
		if (!HttpRequestUtils.checkNotEmptyParam("fav_id", fav_id, response)) {
			return null;
		}
		if (!HttpRequestUtils.checkNeedIntegerParam("fav_id", fav_id, response)) {
			return null;
		}
		ResultObject resultObject = new ResultObject();
		try {
			FavOperateResult result = new FavOperateResult();
			favourableService.modifyFavourableState(fav_id);
			FavourableVO favourable = favourableService.getById(fav_id);
			FavourableEntityVO favourableEntity = favourable
					.getFavourableEntity();
			Date useBegDate = favourableEntity.getUseBegDate();
			Date useEndDate = favourableEntity.getUseEndDate();
			Date nowDate = new Date();
			if (useBegDate != null) {
				if (nowDate.before(useBegDate)) {
					result.setSuccessful(ApiConstant.FALSE_STATUS);
					resultObject.setStatus(ApiConstant.SUCCESS_STATUS);
					resultObject.setMsg(ApiConstant.FAV_USE_TOO_EARLY);
					resultObject.setResult(result);
					HttpRequestUtils.printContent(JsonUtils
							.parseObj2Json(resultObject), response);
					return null;
				}
			}
			if (useEndDate != null) {
				if (nowDate.after(useEndDate)) {
					result.setSuccessful(ApiConstant.FALSE_STATUS);
					resultObject.setStatus(ApiConstant.SUCCESS_STATUS);
					resultObject.setMsg(ApiConstant.FAV_USE_TOO_LATE);
					resultObject.setResult(result);
					HttpRequestUtils.printContent(JsonUtils
							.parseObj2Json(resultObject), response);
					return null;
				}
			}

			// 更新優惠券狀態
			favourableService.modifyFavourableState(favourable.getId()
					.toString());
			Map map = new HashMap();
			map.put("userId", favourable.getUserId());
			map.put("customerId", favourable.getCustomerId());
			// 更新會員卡優惠券數量
			memberCardService.updateCouponAmount(map);
			resultObject.setStatus(ApiConstant.SUCCESS_STATUS);
			resultObject.setMsg(ApiConstant.FAV_USE_SUCCESS);
			result.setSuccessful(ApiConstant.SUCCESS_STATUS);
			resultObject.setResult(result);
		} catch (Exception e) {
			resultObject.setStatus(ApiConstant.FALSE_STATUS);
			resultObject.setMsg(ApiConstant.PUBLIC_EXCEPTION);
			e.printStackTrace();
		}
		HttpRequestUtils.printContent(JsonUtils.parseObj2Json(resultObject),
				response);
		return null;
	}

	public void setFavourableService(IFavourableService favourableService) {
		this.favourableService = favourableService;
	}

	public void setMemberCardService(IMemberCardService memberCardService) {
		this.memberCardService = memberCardService;
	}

}
